const Router = require('@koa/router');
const { checkAuth, checkPermission } = require('../middlewares/auth');
const miniProgramController = require('../controllers/miniProgram.controller');

const router = new Router({ prefix: '/api/v1/mini-programs' });

/**
 * @swagger
 * /api/v1/mini-programs:
 *   post:
 *     tags: [小程序管理]
 *     summary: 创建小程序
 *     description: 创建新的微信小程序配置
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - appId
 *               - appSecret
 *             properties:
 *               name:
 *                 type: string
 *                 description: 小程序名称
 *                 example: "校园助手"
 *               appId:
 *                 type: string
 *                 description: 小程序AppID
 *                 example: "wx1234567890abcdef"
 *               appSecret:
 *                 type: string
 *                 description: 小程���AppSecret
 *                 example: "1234567890abcdef1234567890abcdef"
 *               config:
 *                 type: object
 *                 description: 小程序配置信息
 *                 example: {
 *                   "debug": false,
 *                   "env": "production"
 *                 }
 *               description:
 *                 type: string
 *                 description: 小程序描述
 *                 example: "为校园师生提供便捷服务的小程序"
 *               domain:
 *                 type: string
 *                 description: 小程序业务域名
 *                 example: "https://miniapp.example.com"
 *               serverDomain:
 *                 type: string
 *                 description: 小程序服务器域名
 *                 example: "https://api.example.com"
 *               paymentConfig:
 *                 type: object
 *                 description: 支付配置
 *                 example: {
 *                   "mchId": "1234567890",
 *                   "payKey": "abcdef1234567890"
 *                 }
 *               templateIds:
 *                 type: object
 *                 description: 消息模板ID配置
 *                 example: {
 *                   "orderPaid": "ABC123DEF456",
 *                   "orderShipped": "XYZ789UVW012"
 *                 }
 *               version:
 *                 type: string
 *                 description: 小程序版本号
 *                 example: "1.0.0"
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "mp_001"
 *                 message:
 *                   type: string
 *                   example: "小程序创建成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/', checkAuth, checkPermission('mini_program:create'), miniProgramController.createMiniProgram);

/**
 * @swagger
 * /api/v1/mini-programs:
 *   get:
 *     tags: [小程序管理]
 *     summary: 获取小程序列表
 *     description: 获取所有微信小程序配置列表
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [active, inactive, maintenance]
 *         description: 状态筛选（活跃/停用/维护）
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 10
 *                 pages:
 *                   type: integer
 *                   example: 1
 *                 current:
 *                   type: integer
 *                   example: 1
 *                 miniPrograms:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "mp_001"
 *                       name:
 *                         type: string
 *                         example: "校园助手"
 *                       appId:
 *                         type: string
 *                         example: "wx1234567890abcdef"
 *                       status:
 *                         type: string
 *                         example: "active"
 *                       version:
 *                         type: string
 *                         example: "1.0.0"
 *                       userCount:
 *                         type: integer
 *                         example: 1000
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/', checkAuth, checkPermission('mini_program:read'), miniProgramController.getMiniPrograms);

/**
 * @swagger
 * /api/v1/mini-programs/{id}:
 *   get:
 *     tags: [小程序管理]
 *     summary: 获取小程序详情
 *     description: 获取指定微信小程序的详细配置信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 小程序ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "mp_001"
 *                 name:
 *                   type: string
 *                   example: "校园助手"
 *                 appId:
 *                   type: string
 *                   example: "wx1234567890abcdef"
 *                 config:
 *                   type: object
 *                   example: {
 *                     "debug": false,
 *                     "env": "production"
 *                   }
 *                 description:
 *                   type: string
 *                   example: "为校园师生提供便捷服务的小程序"
 *                 domain:
 *                   type: string
 *                   example: "https://miniapp.example.com"
 *                 serverDomain:
 *                   type: string
 *                   example: "https://api.example.com"
 *                 paymentConfig:
 *                   type: object
 *                   example: {
 *                     "mchId": "1234567890"
 *                   }
 *                 templateIds:
 *                   type: object
 *                   example: {
 *                     "orderPaid": "ABC123DEF456"
 *                   }
 *                 version:
 *                   type: string
 *                   example: "1.0.0"
 *                 status:
 *                   type: string
 *                   example: "active"
 *                 statistics:
 *                   type: object
 *                   properties:
 *                     totalUsers:
 *                       type: integer
 *                       description: 总用户数
 *                       example: 1000
 *                     dailyActiveUsers:
 *                       type: integer
 *                       description: 日活跃用户数
 *                       example: 100
 *                     monthlyActiveUsers:
 *                       type: integer
 *                       description: 月活跃用户数
 *                       example: 500
 *                     totalOrders:
 *                       type: integer
 *                       description: 总订单数
 *                       example: 200
 *                     totalRevenue:
 *                       type: number
 *                       description: 总收入
 *                       example: 10000.00
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 小程序不存在
 */
router.get('/:id', checkAuth, checkPermission('mini_program:read'), miniProgramController.getMiniProgram);

/**
 * @swagger
 * /api/v1/mini-programs/{id}:
 *   put:
 *     tags: [小程序管理]
 *     summary: 更新小程序
 *     description: 更新指定小程序的配置信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 小程序ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 小程序名称
 *               appSecret:
 *                 type: string
 *                 description: 小程序AppSecret
 *               config:
 *                 type: object
 *                 description: 小程序配置信息
 *               description:
 *                 type: string
 *                 description: 小程序描述
 *               domain:
 *                 type: string
 *                 description: 小程序业务域名
 *               serverDomain:
 *                 type: string
 *                 description: 小程序服务器域名
 *               paymentConfig:
 *                 type: object
 *                 description: 支付配置
 *               templateIds:
 *                 type: object
 *                 description: 消息模板ID配置
 *               version:
 *                 type: string
 *                 description: 小程序版本号
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "小程序更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 小程序不存在
 */
router.put('/:id', checkAuth, checkPermission('mini_program:update'), miniProgramController.updateMiniProgram);

/**
 * @swagger
 * /api/v1/mini-programs/{id}:
 *   delete:
 *     tags: [小程序管理]
 *     summary: 删除小程序
 *     description: 删除指定的小程序配置
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 小程序ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "小程序删除成功"
 *       400:
 *         description: 无法删除（小程序正在使用中）
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 小程序不存在
 */
router.delete('/:id', checkAuth, checkPermission('mini_program:delete'), miniProgramController.deleteMiniProgram);

/**
 * @swagger
 * /api/v1/mini-programs/{id}/status:
 *   patch:
 *     tags: [小程序管理]
 *     summary: 更新小程序状态
 *     description: 更新指定小程序的状态
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 小程序ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [active, inactive, maintenance]
 *                 description: 新的状态（活跃/停用/维护）
 *     responses:
 *       200:
 *         description: 状态更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "小程序状态更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 小程序不存在
 */
router.patch('/:id/status', checkAuth, checkPermission('mini_program:update'), miniProgramController.updateMiniProgramStatus);

module.exports = router; 